package com.aaa.service.Impl;

import com.aaa.entity.Menu;
import com.aaa.mapper.MenuMapper;
import com.aaa.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
@Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> listAll() {
        QueryWrapper<Menu> wrapper = new QueryWrapper();
        wrapper.eq("lev", 1);
        List<Menu> oneMenus = baseMapper.selectList(wrapper);
//        遍历一级菜单
        for (Menu oneMenu : oneMenus) {
            QueryWrapper<Menu> wrapper2 = new QueryWrapper();
            wrapper2.eq("lev", 2);
            wrapper2.eq("pid",oneMenu.getMid());
            List<Menu> twoMenus = baseMapper.selectList(wrapper2);
//           将二级菜单放入一级菜单
            oneMenu.setChildren(twoMenus);
        }

        return oneMenus;
    }

    @Override
    public List<Menu> listMyMenus(Long uid) {
//        查询用户能操作的一级菜单
        List<Menu> oneMenus = menuMapper.listMyOneMenus(uid);
        //        遍历一级菜单
        for (Menu oneMenu : oneMenus) {
            List<Menu> twoMenus = menuMapper.listMyTwoMenus(uid,oneMenu.getMid());
//           将二级菜单放入一级菜单
            oneMenu.setChildren(twoMenus);
        }
        return oneMenus;
    }
}
